Deleting objects with links

Hello,
When I delete an object that has links, I want to delete the links first before deleting the object. However, once the links are deleted they show as <unloaded>. What am I doing wrong? Do I need also need to delete out-link before deleting an object?

Below is the code I'm using. Thanks in advance for helping me out.

if (! null object && canDelete(object)) {
Link lnk
for lnk in object->"*" do {
Object targetObject = target(lnk)
if (null targetObject) {
ModName_ moduleRef = target(lnk)
if (fullName(moduleRef) == fullName(m)) {
if (! null m) targetObject = target(lnk)
}
}
if (! null targetObject) {
delete(lnk)
}
}
softDelete(object)
}
SystemAdmin - Sat Mar 19 17:58:14 EDT 2011

Re: Deleting objects with links
SystemAdmin - Sat Mar 19 19:52:35 EDT 2011

I've made some progress. I can now delete the outlinks. I have added flushDeletions().

However, I'm still having problems deleting incoming links. When I have an object with incoming links and I delete that object I get this error: "cannot delete Object: object has incoming links"
How can I fix that and force the deletion?
Thanks for your help.

Re: Deleting objects with links
SystemAdmin - Sun Mar 20 02:02:52 EDT 2011

SystemAdmin - Sat Mar 19 19:52:35 EDT 2011
I've made some progress. I can now delete the outlinks. I have added flushDeletions().

However, I'm still having problems deleting incoming links. When I have an object with incoming links and I delete that object I get this error: "cannot delete Object: object has incoming links"
How can I fix that and force the deletion?
Thanks for your help.

The rules for deleting incoming links are:

1 You must have edit access to the module that is the source of the link
2 The source module of the incoming links must be open in edit mode before you can delete.

The code below is an old utility script of mine and does more than you need, but will give you an idea of what you need to do. This script will attempt to delete all incoming links to the currently selected object.
 

Object o=current
Module m=current
int count=0
 
void deleteIncomingLinks(Object o) {
    Module modSrc=null
        Link l
 
        // Load all source modules
        ModName_ srcModRef
        string srcModName=""
        for srcModRef in o<-"*" do {
                srcModName=fullName(srcModRef)
                modSrc=edit(srcModName, false)
        }
 
        // Delete all incoming links
        if(!null modSrc) { //if modSrc is null, then current user cannot open this mod for edit 
                for l in o<-"*" do {
                        Object objSrc=source l
                        count++
                        delete l
                }
                flushDeletions
        }
        else print "Failed to get access to source module(s) \n"
} //End of deleteIncomingLinks
 
 
//***************** MAIN ********************
 
if(null o) {
        warningBox "No Object has been selected"
        halt
}
 
o = current
if(!null o) {
        if(!confirm "Delete all incoming links to this object?") halt
        deleteIncomingLinks(o)
        infoBox "Deleted " count " links"
        save m
        }
else    {
        warningBox "No object selected"
        }


 

 


Paul Miller
Melbourne, Australia

 

 

Re: Deleting objects with links
SystemAdmin - Sun Mar 20 02:51:07 EDT 2011

SystemAdmin - Sun Mar 20 02:02:52 EDT 2011

The rules for deleting incoming links are:

1 You must have edit access to the module that is the source of the link
2 The source module of the incoming links must be open in edit mode before you can delete.

The code below is an old utility script of mine and does more than you need, but will give you an idea of what you need to do. This script will attempt to delete all incoming links to the currently selected object.
 

Object o=current
Module m=current
int count=0
 
void deleteIncomingLinks(Object o) {
    Module modSrc=null
        Link l
 
        // Load all source modules
        ModName_ srcModRef
        string srcModName=""
        for srcModRef in o<-"*" do {
                srcModName=fullName(srcModRef)
                modSrc=edit(srcModName, false)
        }
 
        // Delete all incoming links
        if(!null modSrc) { //if modSrc is null, then current user cannot open this mod for edit 
                for l in o<-"*" do {
                        Object objSrc=source l
                        count++
                        delete l
                }
                flushDeletions
        }
        else print "Failed to get access to source module(s) \n"
} //End of deleteIncomingLinks
 
 
//***************** MAIN ********************
 
if(null o) {
        warningBox "No Object has been selected"
        halt
}
 
o = current
if(!null o) {
        if(!confirm "Delete all incoming links to this object?") halt
        deleteIncomingLinks(o)
        infoBox "Deleted " count " links"
        save m
        }
else    {
        warningBox "No object selected"
        }


 

 


Paul Miller
Melbourne, Australia

 

 

Had a closer look at the utility script I posted above, it works but there are some flaws, so have posted an improved version - the main changes are an improved method to trap source modules that cannot be opened for edit, and added some code that will save the source modules after the links have been deleted - that's an import aspect to deleting incoming links, you have to save the source modules otherwise the links will not be actually deleted.
 

Object o=current
Module m=current
int count=0
 
void deleteIncomingLinks(Object o) {
    Module modSrc=null
        Module sourceMod=null
        Link l
        bool allModsOpen = true
      ModuleVersion sourceModVersion = null
 
        // Open all modules that are the source of incoming links in edit mode
        ModName_ srcModRef
        string srcModName=""
        for srcModRef in o<-"*" do {
                srcModName=fullName(srcModRef)
                modSrc=edit(srcModName, false)
                if (null modSrc) {  //provide a warning if a source cannot be opened in edit mode
                        infoBox "Cannot open for Edit module: " srcModName
                        allModsOpen = false
                        }
        }
 
        // Delete all incoming links
        if(allModsOpen) {  
                for l in o<-"*" do {
                        Object objSrc=source l
                        sourceModVersion = sourceVersion l
                        srcModRef = module(sourceModVersion)
                        modSrc=edit(fullName(srcModRef), false)
                        count++
                        delete l
                        save modSrc
                }
                flushDeletions
        }
        else print "Failed to get access to source module(s) \n"
} //End of deleteIncomingLinks
 
 
//***************** MAIN ********************
 
if(null o) {
        warningBox "No Object has been selected"
        halt
}
 
o = current
if(!null o) {
        if(!confirm "Delete all incoming links to this object?") halt
        deleteIncomingLinks(o)
        infoBox "Deleted " count " links"
        save m
        }
else    {
        warningBox "No object selected"
        }

 


Paul Miller
Melbourne, Australia

 

Re: Deleting objects with links
SystemAdmin - Sun Mar 20 21:11:49 EDT 2011

SystemAdmin - Sun Mar 20 02:51:07 EDT 2011

Had a closer look at the utility script I posted above, it works but there are some flaws, so have posted an improved version - the main changes are an improved method to trap source modules that cannot be opened for edit, and added some code that will save the source modules after the links have been deleted - that's an import aspect to deleting incoming links, you have to save the source modules otherwise the links will not be actually deleted.
 

Object o=current
Module m=current
int count=0
 
void deleteIncomingLinks(Object o) {
    Module modSrc=null
        Module sourceMod=null
        Link l
        bool allModsOpen = true
      ModuleVersion sourceModVersion = null
 
        // Open all modules that are the source of incoming links in edit mode
        ModName_ srcModRef
        string srcModName=""
        for srcModRef in o<-"*" do {
                srcModName=fullName(srcModRef)
                modSrc=edit(srcModName, false)
                if (null modSrc) {  //provide a warning if a source cannot be opened in edit mode
                        infoBox "Cannot open for Edit module: " srcModName
                        allModsOpen = false
                        }
        }
 
        // Delete all incoming links
        if(allModsOpen) {  
                for l in o<-"*" do {
                        Object objSrc=source l
                        sourceModVersion = sourceVersion l
                        srcModRef = module(sourceModVersion)
                        modSrc=edit(fullName(srcModRef), false)
                        count++
                        delete l
                        save modSrc
                }
                flushDeletions
        }
        else print "Failed to get access to source module(s) \n"
} //End of deleteIncomingLinks
 
 
//***************** MAIN ********************
 
if(null o) {
        warningBox "No Object has been selected"
        halt
}
 
o = current
if(!null o) {
        if(!confirm "Delete all incoming links to this object?") halt
        deleteIncomingLinks(o)
        infoBox "Deleted " count " links"
        save m
        }
else    {
        warningBox "No object selected"
        }

 


Paul Miller
Melbourne, Australia

 

Thank you very much for your post. I got it working now :-)

Re: Deleting objects with links
bmohamed - Sun Apr 24 18:08:35 EDT 2016

SystemAdmin - Sun Mar 20 02:51:07 EDT 2011

Had a closer look at the utility script I posted above, it works but there are some flaws, so have posted an improved version - the main changes are an improved method to trap source modules that cannot be opened for edit, and added some code that will save the source modules after the links have been deleted - that's an import aspect to deleting incoming links, you have to save the source modules otherwise the links will not be actually deleted.
 

Object o=current
Module m=current
int count=0
 
void deleteIncomingLinks(Object o) {
    Module modSrc=null
        Module sourceMod=null
        Link l
        bool allModsOpen = true
      ModuleVersion sourceModVersion = null
 
        // Open all modules that are the source of incoming links in edit mode
        ModName_ srcModRef
        string srcModName=""
        for srcModRef in o<-"*" do {
                srcModName=fullName(srcModRef)
                modSrc=edit(srcModName, false)
                if (null modSrc) {  //provide a warning if a source cannot be opened in edit mode
                        infoBox "Cannot open for Edit module: " srcModName
                        allModsOpen = false
                        }
        }
 
        // Delete all incoming links
        if(allModsOpen) {  
                for l in o<-"*" do {
                        Object objSrc=source l
                        sourceModVersion = sourceVersion l
                        srcModRef = module(sourceModVersion)
                        modSrc=edit(fullName(srcModRef), false)
                        count++
                        delete l
                        save modSrc
                }
                flushDeletions
        }
        else print "Failed to get access to source module(s) \n"
} //End of deleteIncomingLinks
 
 
//***************** MAIN ********************
 
if(null o) {
        warningBox "No Object has been selected"
        halt
}
 
o = current
if(!null o) {
        if(!confirm "Delete all incoming links to this object?") halt
        deleteIncomingLinks(o)
        infoBox "Deleted " count " links"
        save m
        }
else    {
        warningBox "No object selected"
        }

 


Paul Miller
Melbourne, Australia

 

Hi paul,

I tried to adapt your code to delete links on all objects in a module with a certain status with no luck.  i added a basic loop and anything else is above my head frankly.  Would you be able to give me some pointers on how to loop through all objects and just delete the links on certain objects where an attribute meets my criteria?  thanks

for o in m do {
string sstatus = probeAttr_(obj,"Status")
if (sstatus == "Deleted") {
}
                }

 

Re: Deleting objects with links
DOORSHAM - Mon Apr 25 21:58:02 EDT 2016

bmohamed - Sun Apr 24 18:08:35 EDT 2016

Hi paul,

I tried to adapt your code to delete links on all objects in a module with a certain status with no luck.  i added a basic loop and anything else is above my head frankly.  Would you be able to give me some pointers on how to loop through all objects and just delete the links on certain objects where an attribute meets my criteria?  thanks

for o in m do {
string sstatus = probeAttr_(obj,"Status")
if (sstatus == "Deleted") {
}
                }

 

Instead of adapting a script, easy solution may be to create a filter then delete links on remaining objects.

Re: Deleting objects with links
bmohamed - Tue Apr 26 10:45:16 EDT 2016

DOORSHAM - Mon Apr 25 21:58:02 EDT 2016

Instead of adapting a script, easy solution may be to create a filter then delete links on remaining objects.

I created the filter.  The step I want to automate is not having  to click each object one by one to run the deletelinks dxl from the editdxl window if i could.  thanks!  PS...unless i'm misunderstanding your solution, doorsham...